package com.example.flowable.controller;

import org.flowable.engine.*;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping(value = "/expenseGroup2")
public class ExpenseaGroup2Controller {

    @Autowired
    private RuntimeService runtimeService;
    @Autowired
    private TaskService taskService;
    @Autowired
    private RepositoryService repositoryService;
    @Autowired
    private ProcessEngine processEngine;

    @Autowired
    private IdentityService identityService;

    //http://localhost:8181/expenseGroup/start?userId=%E8%B5%B5%E5%85%AD&money=300&descption=500%E5%85%83%E4%BB%A5%E4%B8%8B%E7%9A%84%E6%B5%81%E7%A8%8B%E6%B5%8B%E8%AF%95
    //提交成功.流程Id为：fb73f364-b5c8-11ed-a5b8-f8e4e3dd8a48
/*    @RequestMapping(value = "start", method = RequestMethod.GET)
    public String startExpense(@RequestParam(value = "userId") String userId,
                             @RequestParam(value = "money") String money,
                             @RequestParam(value = "descption") String descption) {
        //启动流程
        HashMap<String, Object> map = new HashMap<>();
        Group group = identityService.createGroupQuery().groupMember(userId).singleResult();
        map.put("sg", group.getId());
        map.put("money", money);
        map.put("descption", descption);
        //ExpenseGroup是我们绘制流程图时自己所填写的key
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("ExpenseGroup", map);
        return "提交成功.流程Id为：" + processInstance.getId();

    }*/

    //http://localhost:8181/expenseGroup/start2?groupId=%E8%B5%B5%E5%85%AD&money=300&descption=500%E5%85%83%E4%BB%A5%E4%B8%8B%E7%9A%84%E6%B5%81%E7%A8%8B%E6%B5%8B%E8%AF%95
    //提交成功.流程Id为：c6516396-b5c9-11ed-99bd-f8e4e3dd8a48
    @RequestMapping(value = "start2", method = RequestMethod.GET)
    public String startExpense2(
                             @RequestParam(value = "money") String money,
                             @RequestParam(value = "descption") String descption) {
        //启动流程
        HashMap<String, Object> map = new HashMap<>();
        map.put("sg", "studentGroup1");  //发起流程组-用户组1, 用户：zhangtongxue，litongxue，wangtongxue
        map.put("sg1", "studentGroup2"); //发起流程组-用户组2，用户：guotongxue，tiantongxue，xuetongxue
        map.put("g1", "manager");  //审批用户组1，用户：zhangmishu、wangmishu
        map.put("g2", "boss");  //审批用户组2，用户：zangyuanzhang
        map.put("money", money);
        map.put("descption", descption);
        //ExpenseGroup是我们绘制流程图时自己所填写的key
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("ExpenseGroup", map);
        return "提交成功.流程Id为：" + processInstance.getId();

    }

    @RequestMapping(value = "/list")
    public Object list(String groupId) {
        List<Task> list = taskService.createTaskQuery()
               // .processDefinitionId("")
                .taskCandidateGroup(groupId)
                .orderByTaskCreateTime().desc().list();
        for (Task task : list) {
            System.out.println(task.toString());
        }
        taskService.createTaskQuery().list();
        return Arrays.toString(list.toArray());
    }

    /**
     * 获取审批管理列表
     */
    @RequestMapping(value = "/list/{userId}")
    public Object listByUserId(@PathVariable("userId") String userId) {
        List<Task> tasks = taskService.createTaskQuery().taskAssignee(userId).orderByTaskCreateTime().desc().list();
        for (Task task : tasks) {
            System.out.println(task.toString());
        }
        return Arrays.toString(tasks.toArray());
    }

    /**
     * 拾取任务
     * @param taskId
     * @param userId
     * @return
     */
    @RequestMapping(value = "/claim")
    public void claimTaskCandidate(String taskId, String userId){
     /*   String groupId = "";
        Task task = taskService.createTaskQuery().taskCandidateGroup(groupId).singleResult();*/
        taskService.claim(taskId, userId);
    }

    /**
     * 退还任务
     * @param taskId
     */
    @RequestMapping(value = "/unclaim")
    public void unclaim(String taskId){
     /*   String groupId = "";
        Task task = taskService.createTaskQuery().taskCandidateGroup(groupId).singleResult();*/
        taskService.unclaim(taskId);
    }

    /**
     * 交接任务
     * @param taskId
     * @param userId
     */
    @RequestMapping(value = "/setAssignee")
    public void setAssignee(String taskId, String userId){
     /*   String groupId = "";
        Task task = taskService.createTaskQuery().taskCandidateGroup(groupId).singleResult();*/
        taskService.setAssignee(taskId, userId);
    }


    @RequestMapping(value = "/complete")
    public String complete(String taskId) {

        Task task = taskService.createTaskQuery().taskId(taskId).singleResult();
        if (task == null) {
            throw new RuntimeException("流程不存在");
        }
        //通过审核
        HashMap<String, Object> map = new HashMap<>();
        map.put("outcome", "通过");
        taskService.complete(taskId, map);
        return "processed ok!";
    }
}
